//
//  DetailBace.m
//  项目
//
//  Created by 焦钟培 on 15-1-6.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailBace.h"
#import "DetailModel.h"
#import "DB.h"

@implementation DetailBace
+ (void)insertIntoDetailWithDetailModel: (DetailModel *)detailModel
{
    sqlite3 *dbpoint = [DB openDB];
    NSString *str = [NSString stringWithFormat:@"insert into detail(detail_id,detail_tit,detail_http) values(%ld,'%@','%@')",detailModel.detail_id,detailModel.detail_tit,detailModel.detail_http];
    int result = sqlite3_exec(dbpoint, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        
    }else{
        
    }
}
+ (void)deleteFromDetailWithDetail_id: (NSInteger)detail_id
{
    sqlite3 *dbpoint = [DB openDB];
    NSString *str = [NSString stringWithFormat:@"delete from detail where detail_id=%ld",detail_id];
    int result = sqlite3_exec(dbpoint, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        
    }else{
        
    }
}

+ (void)updateDetail_tit: (NSString *)detail_tit detail_id: (NSInteger)detail_id detail_http: (NSString *)detail_http
{
    sqlite3 *dbpoint = [DB openDB];
    NSString *str = [NSString stringWithFormat:@"update detail set detail_tit='%@',detail_http='%@' where detail_id=%ld",detail_tit,detail_http,detail_id];
    int result = sqlite3_exec(dbpoint, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    }else{
        NSLog(@"失败");
    }
}

#pragma mark -
#pragma mark 数据查找
+ (NSMutableArray *)searchDetail: (NSInteger)detail_nid
{
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlString = [NSString stringWithFormat:@"select detail_id,detail_tit,detail_http from detail"];
    int result = sqlite3_prepare_v2(dbPoint, [sqlString UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int detail_id = sqlite3_column_int(stmt, 0);
            const unsigned char *detail_tit = sqlite3_column_text(stmt, 1);
            const unsigned char *detail_http = sqlite3_column_text(stmt, 2);

            NSString *detail_tit1 = [NSString stringWithUTF8String:(const char *)detail_tit];
            NSString *detail_http1 = [NSString stringWithUTF8String:(const char *)detail_http];

            DetailModel *model = [DetailModel detailModelWithDetail_id:detail_id detail_tit:detail_tit1 detail_http:detail_http1];
            if (model.detail_id == (int)detail_nid) {
                [array addObject:model];
            }
        }
    }
    sqlite3_finalize(stmt);
    return array;
}

+ (void)deleteAll
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlString = [NSString stringWithFormat:@"DROP TABLE detail"];
    int result = sqlite3_exec(dbPoint, [sqlString UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"列表删除成功");
    }else{
        NSLog(@"列表删除失败");
    }

    sqlite3 *dbPoint1 = [DB openDB];
    NSString *sqlString1 = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS detail(detail_id INTEGER PRIMARY KEY AUTOINCREMENT, detail_tit TEXT, detail_http TEXT)"];
    NSLog(@"%@", sqlString);
    int result1 = sqlite3_exec(dbPoint1, [sqlString1 UTF8String], NULL, NULL, nil);
    if (result1 == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建表失败");
    }
}

@end
